package com.cskaoyan.gateway.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.user.IUserLoginService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.ILoginService;
import com.mall.user.annotation.Anoymous;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 创建日期: 2022-01-19-22-59
 * author : WQW
 * description: 用户登录
 */

/**
 * 创建日期: 2022-01-19-22-49
 * author : LL
 * description:
 */
//@Anoymous
@Slf4j
@RestController
@RequestMapping("/user")
public class LoginController {

    @Reference(timeout = 3000, check = false)
    IUserLoginService userLoginService;

    @Reference(timeout = 3000,check = false)
    IKaptchaService kaptchaService;

    @Reference(timeout = 3000,check = false)
    ILoginService loginService;

    @PostMapping("/login")
    @Anoymous
    public ResponseData login(@RequestBody Map<String,String> map, HttpServletRequest request, HttpServletResponse response){
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String captcha = map.get("captcha");

        // 1.验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse kaptchaCodeResponse = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        if (!kaptchaCodeResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());
        }
        // 2.验证用户名以及密码
        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setUserName(userName);
        userLoginRequest.setPassword(userPwd);
        UserLoginResponse loginResponse = loginService.login(userLoginRequest);
        if (!loginResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil<>().setErrorMsg(loginResponse.getMsg());
        }
        Cookie cookie = CookieUtil.genCookie(TokenIntercepter.ACCESS_TOKEN,loginResponse.getToken(),"/",6000);
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
        return new ResponseUtil<>().setData(loginResponse);
    }

//    @Anoymous
//    @PostMapping("/login")
//    public ResponseData userLogin(UserLoginRequest loginRequest){
//
//        UserLoginResponse loginResponse = userLoginService.userLogin(loginRequest);
//        if (loginResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
//            return new ResponseUtil().setData(loginResponse);
//        }


//        return null;
//    }


    @GetMapping("/login")
    public ResponseData verifyLogin(HttpServletRequest httpServletRequest) {
        String userinfo = (String) httpServletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        if (userinfo == null || userinfo.length() == 0) {
            return new ResponseUtil<>().setErrorMsg(SysRetCodeConstants.TOKEN_VALID_FAILED.getMessage());
        }
        JSONObject userInfo = JSON.parseObject(userinfo);
        Long uid = Long.parseLong(userInfo.get("uid").toString());
        String file = (String) userInfo.get("file");
        UserInfoDto userInfoDto = new UserInfoDto();
//        UserinfoResponse userinfoResponse = new UserinfoResponse();
        userInfoDto.setUid(uid);
        userInfoDto.setFile(file);
        return new ResponseUtil().setData(userInfoDto);
    }

    @GetMapping("/loginOut")
    public ResponseData loginOut(HttpServletResponse response) {
        // httpServletRequest.setAttribute(TokenIntercepter.USER_INFO_KEY, null);
        // 1.退出登录逻辑很简单, 清除浏览器Cookie信息
        Cookie cookie = CookieUtil.genCookie("access_token", "", "/", 0);
        response.addCookie(cookie);
        // 执行成功
        return new ResponseUtil<>().setData(null);
    }

    @GetMapping("/verify")
    @Anoymous
    public ResponseData verifyAccount(UserVerifyRequest userVerifyRequest) {
        UserVerifyResponse userVerifyResponse = userLoginService.userVerify(userVerifyRequest);
        if (userVerifyResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(userVerifyResponse.getMsg());
    }

}